﻿package com.yy.ui.controls {
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import com.fminutes.vinciUI.events.*;
    import gs.*;
    import com.fminutes.vinciUI.core.*;
    import com.fminutes.vinciUI.containers.*;

    public class List extends UIComponent {

        private var dataArray:Array;
        private var index:int;
        private var itemWidth:int;
        private var itemHeight:int;
        private var listContainer:Sprite;
        private var listContainerWidth:Number;
        private var scrollContainer:Box;
        private var listItem:Class;
        private var itemArray:Array;
        private var pageItemNum:int;
        private var page:int;
        private var totalPage:int;
        private var scrollWidth:Number;
        private var tweening:Boolean;
        private var _itemDistance:int = 15;
        private var _selectedIndx:int;
        private var _selectedItem:DisplayObject;
        private var rollDirection:int;
        private var isFillData:Boolean;
        private var itemRow:int;
        private var _w:Number;
        private var _h:Number;
        private var timer:Timer;

        public function List(_arg1:Class, _arg2:Array, _arg3:int, _arg4:Number, _arg5:Number, _arg6:Number, _arg7:Boolean=false){
            this.width = _arg6;
            this.isFillData = _arg7;
            this.dataArray = _arg2;
            this.fillSelectedFlag();
            this.itemWidth = _arg4;
            this.itemHeight = _arg5;
            this.listItem = _arg1;
            this.itemDistance = _arg3;
            this.index = 0;
            this.timer = new Timer(10);
            this.timer.addEventListener(TimerEvent.TIMER, this.timerProgress);
            this.init();
        }
        private function init():void{
            this.creatItemList();
        }
        public function setListData(_arg1:Array, _arg2:Boolean=true):void{
            var _local3:int;
            var _local4:int;
            var _local5:int;
            TweenLite.killTweensOf(this.scrollContainer, true);
            this.dataArray = _arg1.concat();
            this.fillSelectedFlag();
            if (this.isFillData){
                _local3 = (this.dataArray.length % this.itemRow);
                this.dataArray.length = (this.dataArray.length + (this.itemRow - _local3));
            };
            this.listContainerWidth = (this.dataArray.length * this.scrollWidth);
            if (_arg2){
                this.scrollContainer.cX = 0;
                this.index = 0;
                _local4 = 0;
                while (_local4 < this.itemArray.length) {
                    this.itemArray[_local4].x = (_local4 * this.scrollWidth);
                    this.itemArray[_local4].index = _local4;
                    this.itemArray[_local4].data = this.dataArray[_local4];
                    _local4++;
                };
            } else {
                _local5 = 0;
                while (_local5 < this.itemArray.length) {
                    this.itemArray[_local5].x = ((this.index + _local5) * this.scrollWidth);
                    this.itemArray[_local5].index = (this.index + _local5);
                    this.itemArray[_local5].data = this.dataArray[(this.index + _local5)];
                    _local5++;
                };
            };
        }
        public function get itemList():Array{
            return (this.itemArray);
        }
        public function scrollList(_arg1:int):Boolean{
            var _local2:Number;
            trace("tweening.....", this.tweening);
            if (this.tweening){
                return (false);
            };
            if (_arg1 > 0){
                _local2 = (Math.floor(((this.listContainerWidth - this.scrollContainer.cX) / this.scrollWidth)) * 0.3);
            } else {
                _local2 = (Math.floor((this.scrollContainer.cX / this.scrollWidth)) * 0.3);
            };
            if (this.scrollContainer != null){
                this.rollDirection = _arg1;
                if ((((this.scrollContainer.cX < (this.listContainerWidth - (this.scrollWidth * this.pageItemNum)))) && ((this.rollDirection > 0)))){
                    if (!this.canScrollItemNext()){
                        return (false);
                    };
                    this.tweening = true;
                    TweenLite.to(this.scrollContainer, _local2, {
                        cX:int((this.listContainerWidth - (this.scrollWidth * this.pageItemNum))),
                        onComplete:this.func,
                        overwrite:1
                    });
                } else {
                    if ((((this.scrollContainer.cX > 0)) && ((this.rollDirection < 0)))){
                        if (!this.canScrollItemPrev()){
                            return (false);
                        };
                        this.tweening = true;
                        TweenLite.to(this.scrollContainer, _local2, {
                            cX:0,
                            onComplete:this.func,
                            overwrite:1
                        });
                    };
                };
                return (true);
            };
            return (false);
        }
        private function func():void{
            this.dispatchRollCompleteEvent();
        }
        public function stopRoll(_arg1:Number=0.5):void{
            if (!this.tweening){
                return;
            };
            var _local2:Number = ((Math.floor((this.scrollContainer.cX / this.scrollWidth)) * this.scrollWidth) + this.scrollWidth);
            if ((((this.scrollContainer.cX >= (this.listContainerWidth - (this.scrollWidth * this.pageItemNum)))) || ((this.scrollContainer.cX <= 0)))){
                this.func();
            } else {
                TweenLite.to(this.scrollContainer, _arg1, {
                    cX:((this.rollDirection)>0) ? _local2 : (_local2 - this.scrollWidth),
                    onComplete:this.func
                });
            };
        }
        public function getSrollDistance():Number{
            if (((this.dataArray.length - this.index) - this.pageItemNum) >= 0){
                return ((((this.dataArray.length - this.index) - this.pageItemNum) * this.scrollWidth));
            };
            return (((this.dataArray.length - this.index) * this.scrollWidth));
        }
        public function searchItemByName(_arg1:String):void{
        }
        public function gotoPage(_arg1:int):void{
        }
        private function resetIndex():void{
        }
        public function get selectedIndx():int{
            return (this._selectedIndx);
        }
        public function set selectedIndx(_arg1:int):void{
            this._selectedIndx = _arg1;
            this.fillSelectedFlag();
            var _local2:int;
            while (_local2 < this.itemArray.length) {
                if (!this.itemArray[_local2].data){
                } else {
                    if (this.itemArray[_local2].data["selectedFlag"]){
                        this.itemArray[_local2].selected = true;
                    } else {
                        this.itemArray[_local2].selected = false;
                    };
                };
                _local2++;
            };
        }
        public function get selectedItem():DisplayObject{
            var _local1:int;
            while (_local1 < this.itemArray.length) {
                if (this.itemArray[_local1].selected){
                    this._selectedItem = this.itemArray[_local1];
                    return (this._selectedItem);
                };
                _local1++;
            };
            return (null);
        }
        private function changeSelectedItem(_arg1:UIEvent=null):void{
            var _local2:int;
            while (_local2 < this.itemArray.length) {
                if (this.itemArray[_local2] == _arg1.target){
                    this.selectedItem = this.itemArray[_local2];
                } else {
                    this.itemArray[_local2].selected = false;
                };
                _local2++;
            };
            var _local3:UIEvent = new UIEvent(UIEvent.CHANGE, false, false, ListItem(this.selectedItem).data);
            dispatchEvent(_local3);
        }
        public function set selectedItem(_arg1:DisplayObject):void{
            this._selectedItem = _arg1;
        }
        public function get itemDistance():int{
            return (this._itemDistance);
        }
        public function set itemDistance(_arg1:int):void{
            this._itemDistance = _arg1;
        }
        override public function updateUI():void{
            super.updateUI();
        }
        private function creatItemList():void{
            var _local3:int;
            var _local4:ListItem;
            this.itemArray = [];
            this.itemRow = Math.floor((this.width / (this.itemWidth + this.itemDistance)));
            this.itemDistance = ((((this.width - ((this.itemWidth + this.itemDistance) * this.itemRow)) + this.itemDistance) / this.itemRow) + this.itemDistance);
            if (this.isFillData){
                _local3 = (this.dataArray.length % this.itemRow);
                this.dataArray.length = (this.dataArray.length + (this.itemRow - _local3));
            };
            this.scrollContainer = new Box();
            this.scrollWidth = (this.itemWidth + this.itemDistance);
            this.pageItemNum = this.itemRow;
            this.listContainerWidth = (this.dataArray.length * this.scrollWidth);
            this.listContainer = new Sprite();
            var _local1:int;
            while (_local1 < (this.itemRow * 3)) {
                _local4 = (new this.listItem() as ListItem);
                _local4.x = (_local1 * this.scrollWidth);
                _local4.index = _local1;
                _local4.buttonMode = true;
                this.itemArray.push(_local4);
                _local4.addEventListener(UIEvent.LISTITEM_SELECTED_CHANGE, this.changeSelectedItem);
                _local4.addEventListener(UIEvent.LISTITEM_CLICK, this.itemClick);
                this.listContainer.addChild(_local4);
                _local1++;
            };
            this.scrollContainer.addChild(this.listContainer);
            this.scrollContainer.width = this.width;
            this.scrollContainer.height = this.height;
            this.scrollContainer.addEventListener(UIEvent.ROLL, this.updateItem);
            addChild(this.scrollContainer);
            var _local2:int;
            while (_local2 < this.dataArray.length) {
                if (_local2 < this.itemArray.length){
                    if (this.itemArray[_local2].index == _local2){
                        this.itemArray[_local2].data = this.dataArray[_local2];
                    };
                };
                _local2++;
            };
        }
        protected function itemClick(_arg1:MouseEvent):void{
            ListItem(_arg1.target).selected = true;
        }
        override public function set width(_arg1:Number):void{
            this._w = _arg1;
            if (this.scrollContainer){
                this.scrollContainer.width = _arg1;
            };
            this.updateUI();
        }
        override public function get width():Number{
            return (this._w);
        }
        override public function set height(_arg1:Number):void{
            this._h = _arg1;
            if (this.scrollContainer){
                this.scrollContainer.height = _arg1;
            };
        }
        override public function get height():Number{
            return (this._h);
        }
        private function timerProgress(_arg1:TimerEvent):void{
        }
        public function nextPage():void{
            var func:* = null;
            func = function ():void{
                index = (index + pageItemNum);
                tweening = false;
                dispatchRollCompleteEvent();
            };
            if (!this.canScrollPageNext()){
                return;
            };
            if (!this.tweening){
                if ((this.listContainerWidth - this.scrollContainer.cX) > ((this.scrollWidth * this.pageItemNum) * 2)){
                    TweenLite.to(this.scrollContainer, 0.8, {
                        cX:(this.scrollContainer.cX + (this.scrollWidth * this.pageItemNum)),
                        onComplete:func
                    });
                    this.tweening = true;
                } else {
                    TweenLite.to(this.scrollContainer, 0.8, {
                        cX:(this.listContainerWidth - (this.scrollWidth * this.pageItemNum)),
                        onComplete:func
                    });
                    this.tweening = true;
                };
            };
        }
        public function prevPage():void{
            var func:* = null;
            func = function (_arg1:String=""):void{
                if (_arg1 == "index"){
                    index = (index - index);
                } else {
                    index = (index - pageItemNum);
                };
                tweening = false;
                dispatchRollCompleteEvent();
            };
            if (!this.canScrollPagePrev()){
                return;
            };
            if (this.tweening){
                return;
            };
            if (!this.tweening){
                if (this.index <= this.pageItemNum){
                    TweenLite.to(this.scrollContainer, 0.8, {
                        cX:(this.scrollContainer.cX - (this.scrollWidth * this.index)),
                        onComplete:func,
                        onCompleteParams:["index"]
                    });
                    this.tweening = true;
                } else {
                    TweenLite.to(this.scrollContainer, 0.8, {
                        cX:(this.scrollContainer.cX - (this.scrollWidth * this.pageItemNum)),
                        onComplete:func
                    });
                    this.tweening = true;
                };
            };
        }
        public function nextItem():void{
            var func:* = null;
            func = function ():void{
                index++;
                tweening = false;
                dispatchRollCompleteEvent();
            };
            if (!this.canScrollItemNext()){
                return;
            };
            if (!this.tweening){
                TweenLite.to(this.scrollContainer, 0.8, {
                    cX:(this.scrollContainer.cX + this.scrollWidth),
                    onComplete:func
                });
                this.tweening = true;
            };
        }
        public function prevItem():void{
            var func:* = null;
            func = function ():void{
                index--;
                tweening = false;
                dispatchRollCompleteEvent();
            };
            if (!this.canScrollItemPrev()){
                return;
            };
            if (!this.tweening){
                TweenLite.to(this.scrollContainer, 0.8, {
                    cX:(this.scrollContainer.cX - this.scrollWidth),
                    onComplete:func
                });
                this.tweening = true;
            };
        }
        public function canScrollItemNext():Boolean{
            this.updateIndex();
            if (this.index >= (this.dataArray.length - this.pageItemNum)){
                return (false);
            };
            return (true);
        }
        public function canScrollItemPrev():Boolean{
            this.updateIndex();
            if (this.index == 0){
                return (false);
            };
            return (true);
        }
        public function canScrollPageNext():Boolean{
            this.updateIndex();
            if (this.index >= (this.dataArray.length - this.pageItemNum)){
                return (false);
            };
            return (true);
        }
        public function canScrollPagePrev():Boolean{
            this.updateIndex();
            if (this.index == 0){
                return (false);
            };
            return (true);
        }
        private function updateItem(_arg1:UIEvent=null):void{
            var _local7:int;
            var _local8:int;
            var _local2:Array = [];
            var _local3:Array = [];
            var _local4:int;
            var _local5:int;
            while (_local5 < this.itemArray.length) {
                _local8 = (DisplayObject(this.itemArray[_local5]).x - this.scrollContainer.cX);
                if (_local8 <= (-(this.scrollWidth) * this.pageItemNum)){
                    this.itemArray[_local5].x = (this.itemArray[(this.itemArray.length - 1)].x + (this.scrollWidth * (_local5 + 1)));
                    _local2.push(this.itemArray[_local5]);
                };
                if (_local8 >= ((this.scrollWidth * this.pageItemNum) * 2)){
                    this.itemArray[_local5].x = (this.itemArray[0].x - (this.scrollWidth * (_local4 + 1)));
                    _local3.push(this.itemArray[_local5]);
                    _local4++;
                };
                _local5++;
            };
            var _local6:int;
            while (_local6 < _local2.length) {
                this.itemArray.push(_local2[_local6]);
                this.itemArray[(this.itemArray.length - 1)].index = (this.itemArray[(this.itemArray.length - 2)].index + 1);
                this.itemArray[(this.itemArray.length - 1)].data = this.dataArray[this.itemArray[(this.itemArray.length - 1)].index];
                if (this.itemArray[(this.itemArray.length - 1)].data){
                    this.itemArray[(this.itemArray.length - 1)].selected = this.itemArray[_local5].data["selectedFlag"];
                };
                this.itemArray.shift();
                _local6++;
            };
            _local7 = 0;
            while (_local7 < _local3.length) {
                if ((_local3[_local7].x + this.scrollWidth) >= 0){
                    _local3[_local7].index = (((_local3[_local7].x + this.scrollWidth) / this.scrollWidth) - 1);
                    _local3[_local7].data = this.dataArray[_local3[_local7].index];
                    if (_local3[_local7].data){
                        _local3[_local7].selected = _local3[_local7].data["selectedFlag"];
                    };
                };
                this.itemArray.unshift(_local3[_local7]);
                _local7++;
            };
            this.itemArray.splice((this.itemArray.length - _local7), _local7);
            this.updateIndex();
        }
        private function updateIndex():void{
            this.index = Math.ceil((this.scrollContainer.cX / this.scrollWidth));
        }
        private function dispatchRollCompleteEvent():void{
            this.tweening = false;
            dispatchEvent(new UIEvent(UIEvent.ROLL_COMPLETE));
        }
        private function fillSelectedFlag():void{
            var _local1:int;
            while (_local1 < this.dataArray.length) {
                if (this.dataArray[_local1]){
                    if (_local1 == this.selectedIndx){
                        this.dataArray[_local1]["selectedFlag"] = true;
                    } else {
                        this.dataArray[_local1]["selectedFlag"] = false;
                    };
                };
                _local1++;
            };
        }

    }
}